//
//  TLHomeVC.m
//  TLLoanSimulat
//
//  Created by mc on 2017/10/20.
//  Copyright © 2017年 mc. All rights reserved.
//

#import "TLHomeVC.h"
#import "TLHomeCell.h"
#import "TLHomeHeaderView.h"
#import "TLHomeFooterView.h"

#import "XRTabbarVC.h"

#define kCollCellID @"HomeCell"
#define kCellCoReuseHeader @"HomeHeaderView"
#define kCellCoReuseFooter @"HomeFooterView"

@interface TLHomeVC ()<UICollectionViewDelegate,UICollectionViewDataSource> {
    NSMutableArray *_dataArray;
}
@property (unsafe_unretained, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong,nonatomic)TLHomeHeaderView *headerView;
@property (strong,nonatomic)TLHomeFooterView *footerView;

@end

@implementation TLHomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self initData];
    // Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 初始化
#pragma mark -
#pragma mark ---初始化UI
-(void)initUI {
    [self initCollect];
}
#pragma mark ---初始化数据
-(void)initData {
    [_collectionView reloadData];
}
#pragma mark - Collection
#pragma mark -
#pragma mark ---初始化collectionView
-(void)initCollect
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    CGFloat itemWH = (SCREEN_WIDTH-1)/2;
    //设置cell的尺寸
    [flowLayout setItemSize:CGSizeMake(itemWH,80)];
    //设置最小边距
    [flowLayout setMinimumLineSpacing:1];//纵向
    [flowLayout setMinimumInteritemSpacing:1];//横向
    //设置其布局方向
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    [_collectionView setCollectionViewLayout:flowLayout];
    _collectionView.delegate=self;
    _collectionView.dataSource=self;
    [_collectionView setBackgroundColor:[UIColor clearColor]];
    [self registerCollection];
//    [self setUpMJRefresh];
}



#pragma mark - 列表相关
#pragma mark -
#pragma mark ---注册CELL
- (void)registerCollection
{
    //注册 cell
    [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([TLHomeCell class]) bundle:nil] forCellWithReuseIdentifier:kCollCellID];
    
    [_collectionView registerNib:[UINib nibWithNibName:@"TLHomeHeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCellCoReuseHeader];
    [_collectionView registerNib:[UINib nibWithNibName:@"TLHomeFooterView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kCellCoReuseFooter];
}
#pragma mark ---选择器

//这个方法是返回 Header的大小 size
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(SCREEN_WIDTH, 210);
}
//返回头footerView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeMake(SCREEN_WIDTH, 40);
}
//这个也是最重要的方法 获取Header的 方法。
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString: UICollectionElementKindSectionHeader]) {
        //从缓存中获取 Footer
        if (!_headerView) {
//            kWeakSelf(self)
            _headerView = (TLHomeHeaderView *)[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:kCellCoReuseHeader forIndexPath:indexPath];
        }
//        [_headerView setHeaderWithModel:nil];
        return _headerView;
    }else if([kind isEqualToString: UICollectionElementKindSectionFooter]){
        //从缓存中获取 Footer
        if (!_footerView) {
            _footerView = (TLHomeFooterView *)[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:kCellCoReuseFooter forIndexPath:indexPath];
        }
        return _footerView;
    }else {
        return nil;
    }
}

-(void)pushToLogin {
//    [self navigationPushViewController:[TLLoginVC class] animation:YES withOperation:^(id vcInstance) {
//        TLLoginVC *vc = vcInstance;
//        vc.popToVC = self;
//        [vc setLoginSuc:^{
//            [self getReqForLoanScore];
//        }];
//    }];
}

#pragma mark ---选择器中cell个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 5;
}

#pragma mark ---选择器 section个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

#pragma mark ---选择器cell大小
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    return CGSizeMake((SCREEN_WIDTH)/4,(SCREEN_WIDTH)/4);
//}


#pragma mark ---设置选择器的cell间隔
//-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
//{
//    return UIEdgeInsetsMake(0,0,0,0);
//}

#pragma mark ---选择器展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TLHomeCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollCellID forIndexPath:indexPath];
    if (!cell) {
        cell= [collectionView dequeueReusableCellWithReuseIdentifier:kCollCellID forIndexPath:indexPath];
    }
//    [cell setCellWithModel:_dataArray[indexPath.row]];
    return cell;
}

#pragma mark ---选择器的点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    [self presentViewController:[XRTabbarVC class] animation:YES];
    
}

@end
